#!/bin/bash

nexus_address="10.2.158.42:8081"
nexus_user=admin
nexus_pwd=xxx

ip=10.2.158.90
port=3306
runuser=ampdcp
path=/home/ampdcp


type=mysql

# 下载
rm -rf /tmp/$type.tar.gz
a="curl -s -u $nexus_user:$nexus_pwd -o /tmp/$type.tar.gz http://$nexus_address/repository/atmpack/database/$type.tar.gz"
echo "开始下载: $a"

curl -s -u $nexus_user:"$nexus_pwd" -o /tmp/$type.tar.gz http://$nexus_address/repository/atmpack/database/$type.tar.gz
if [ $? != 0 ]; then
	echo "download faild!"
	exit 1
else
	echo "下载完成"
fi

# 安装
tar -zxvf /tmp/$type.tar.gz -C $path/
if [ $? != 0 ]; then
	echo "解压失败"
	exit 1
else
	echo "解压完成"
fi

cat >> $path/$type/my.cnf << EOF
[mysql]
port=$port
socket=$path/$type/mysql.sock
default-character-set=utf8

[client]
socket=$path/$type/mysql.sock
[mysqld_safe]
log-error=$path/$type/data/mysql.error

[mysqld]
port=$port
user=$runuser
basedir=$path/$type
datadir=$path/$type/data
socket=$path/$type/mysql.sock
pid-file=$path/$type/data/mysql.pid
character-set-server=utf8
skip-name-resolve
log_bin=ON  
#log_bin=$path/$type/data/log/mysql-bin  
#log_bin_index=$path/$type/data/log1/mysql-bin.index 

expire_logs_days=7
server-id=43
binlog_format = ROW
lower_case_table_names=1
#query_cache_size=0
transaction_isolation=READ-COMMITTED
#tmp_table_size=128M
tmp_table_size=512M
#max_heap_table_size=96M
max_heap_table_size=256M
max_connections=10000
max_connect_errors=6000
max_allowed_packet=512M
#long_query_time=1
innodb_buffer_pool_size=32G
innodb_flush_log_at_trx_commit=0
innodb_log_buffer_size=8M
innodb_log_file_size=128M
innodb_log_files_in_group=2
innodb_file_per_table=1
innodb_flush_method=O_DIRECT
innodb_file_format=Barracuda
innodb_write_io_threads=8
innodb_read_io_threads=4
innodb_doublewrite=0
innodb_purge_threads=1
innodb_stats_on_metadata=OFF
innodb_io_capacity=1000
#log-bin-trust-function-creators=1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER
event_scheduler=on
log-error=$path/$type/data/mysql.error
EOF
echo "$path/$type/my.cnf"配置文件生成完成
cat $path/$type/my.cnf

chown -R $runuser:$runuser $path/$type

echo "开始初始化数据库"
$path/$type/bin/mysqld --defaults-file=$path/$type/my.cnf --initialize --user=$runuser

echo "初始化完成"

#获取密码
ori_pwd=`grep "A temporary password" $path/$type/data/mysql.error | awk -F ': ' '{print $2}'`

# 启动
sed -i "s#basedir=/home/ampdcp/snc_product/mysql#basedir=$path/$type#g" $path/$type/mysqld.sh
sed -i "s#datadir=/home/ampdcp/snc_product/mysql/data#datadir=$path/$type/data#" $path/$type/mysqld.sh
sed -i "s#conf=/home/ampdcp/snc_product/mysql/my.cnf#conf=$path/$type/my.cnf#g" $path/$type/mysqld.sh
chown -R $runuser:$runuser $path/$type
sh $path/$type/mysqld.sh start
echo "MySQL启动完成,原密码：$ori_pwd"
